<?php


namespace App\Http\Controllers\Admin;


use App\Http\Controllers\BaseController;
use App\Models\Client;
use App\Models\Event;
use Illuminate\Http\Request;

class ClientController extends BaseController
{
    /**
     * 获取客户列表 /client
     * @return \Illuminate\Contracts\Foundation\Application|\Illuminate\Contracts\Routing\ResponseFactory|\Illuminate\Http\Response
     */
    public function getClientList(){
        $client_data = Client::select('id', 'corporate_name', 'firstname', 'lastname', 'phone', 'email')->get();
        return $this->returnJson(BaseController::OK, 'success', $client_data);
    }

    /**
     * 通过ID获取客户 /client/{client_id}
     * @param $client_id
     * @return \Illuminate\Contracts\Foundation\Application|\Illuminate\Contracts\Routing\ResponseFactory|\Illuminate\Http\Response
     */
    public function getClientInfo($client_id){
        $client_data = Client::where('id', $client_id)->select('id', 'corporate_name', 'firstname', 'lastname', 'phone', 'email')->first();

        //判断是否该客户
        if (!$client_data){
            return $this->returnJson(BaseController::NOTFOUND, 'not found');
        }

        return $this->returnJson(BaseController::OK, 'success', $client_data);
    }

    /**
     * 删除客户 /client/{client_id}
     * @param $client_id
     * @return \Illuminate\Contracts\Foundation\Application|\Illuminate\Contracts\Routing\ResponseFactory|\Illuminate\Http\Response
     */
    public function deleteClient($client_id){
        //判断该客户是否存在
        $client_data = Client::where('id', $client_id)->first();
        if (!$client_data){
            return $this->returnJson(BaseController::NOTFOUND, 'not found');
        }

        //判断该客户是否关联事件
        $client_event = Event::where('client_id', $client_id)->first();
        if ($client_event){
            return $this->returnJson(BaseController::REQUIRED_FIELD_EMPTY, 'Client still has related events');
        }

        //删除客户
        Client::where('id', $client_id)->delete();
        return $this->returnJson(BaseController::OK, 'success');
    }



    /**
     *  添加新客户 /client
     * @param Request $request
     * @return \Illuminate\Contracts\Foundation\Application|\Illuminate\Contracts\Routing\ResponseFactory|\Illuminate\Http\Response
     */
    public function addClient(Request $request){
        $params = $request->only('corporate_name', 'firstname', 'lastname', 'email', 'phone');

        //判断为空
        if (empty($params['corporate_name']) || empty($params['firstname']) || empty($params['lastname'])
            || empty($params['email']) || empty($params['phone'])){
            return $this->returnJson(BaseController::REQUIRED_FIELD_EMPTY, 'data cannot be processed');
        }

        //判断客户是否存在
        $is_corporate_name_exists = Client::where([
            'corporate_name' => $params['corporate_name'],
            'email'          => $params['email']
        ])->first();

        if ($is_corporate_name_exists){
            return $this->returnJson(BaseController::REQUIRED_FIELD_EMPTY, 'This Corporate name has already been taken');
        }

        $id = Client::insertGetId($params);
        return $this->returnJson(BaseController::OK, 'success', $id);
    }
}
